<?php

// +----------------------------------------------------------------------
// | Token api控制器
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

namespace app\api\controller\v1;

use app\api\validate\Token as TokenValidate;
use app\api\validate\AppToken as AppTokenValidate;
use app\api\service\UserToken;
use app\api\service\AppToken;
use app\common\lib\exception\ApiException;
use app\api\service\Token as TokenService;

class Token
{
    /**
    * 获取用户的token
    * @http post 
    * @param string $code 用户code码 
    * @return string 用户token
    */
    public function getToken($code='')
    {
        (new TokenValidate())->goCheck();

        $ut = new UserToken($code);

        $token = $ut->get();
        
        return show(config('api.success_status'),'ok',['token'=>$token]);
    }
    /**
    * 验证token
    * @http post 
    * @param string $token 用户token
    * @return array 验证结果
    */
    public function verifyToken($token='')
    {
        if(!$token){
            throw new ApiException("token不能为空", 400,10003);
        }

        $res = TokenService::verifyToken($token);
        return json([
            'valid'=>$res
        ]);
    }
    /**
    * 第三方应用获取token
    * @http post 
    * @param string ac 账号 se密码
    * @return array 
    */
    public function getAppToken($ac='',$se='')
    {
        (new AppTokenValidate())->goCheck();
        $app = new AppToken();
        $token = $app->get($ac, $se);
        return json([
            'token' => $token
        ]);
    }
}
